package com.whately.core.database.common.srv;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.whately.core.database.common.dal.mapper.CommonDicDtlMapper;
import com.whately.core.database.common.dal.mapper.CommonDicMapper;
import com.whately.core.database.common.dal.model.CommonDic;
import com.whately.core.database.common.dal.model.CommonDicDtl;
import com.whately.core.database.common.dal.model.CommonDicDtlExample;
import com.whately.core.database.common.dal.model.CommonDicExample;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 默认不开启事务
 */
@Service
@Slf4j
public class CoreDicFactory {

    @Resource
    private CommonDicMapper commonDicMapper;

    @Resource
    private CommonDicDtlMapper commonDicDtlMapper;

    /**********************************************
     *   字典
     *************************************/
    //不开启事务
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public CommonDic dicByCode(String dicCode) {
        return commonDicMapper.selectByPrimaryKey(dicCode);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<CommonDic> dicByCodeList(List<String> dicCodeList) {
        if(CollUtil.isEmpty(dicCodeList)){
            return Lists.newArrayList();
        }
        CommonDicExample example = new CommonDicExample();
        example.createCriteria().andDicCodeIn(dicCodeList);
        return commonDicMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<CommonDic> dicAll() {
        CommonDicExample example = new CommonDicExample();
        return commonDicMapper.selectByExample(example);
    }

    /**********************************************
     *   字典明细
     *************************************/
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<CommonDicDtl> detailsByCode(String dicCode) {
        CommonDicDtlExample example = new CommonDicDtlExample();
        example.createCriteria().andDicCodeEqualTo(dicCode);
        example.setOrderByClause(" seq_no ");
        return commonDicDtlMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<CommonDicDtl> detailsByCodeList(List<String> dicCodeList) {
        if(CollUtil.isEmpty(dicCodeList)){
            return Lists.newArrayList();
        }
        CommonDicDtlExample example = new CommonDicDtlExample();
        example.createCriteria().andDicCodeIn(dicCodeList);
        example.setOrderByClause(" dic_code,seq_no ");
        return commonDicDtlMapper.selectByExample(example);
    }


    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<CommonDicDtl> detailsAll() {
        CommonDicDtlExample example = new CommonDicDtlExample();
        example.setOrderByClause(" dic_code,seq_no ");
        return commonDicDtlMapper.selectByExample(example);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public CommonDicDtl selectDetailByPrimary(String dtlId) {
        return commonDicDtlMapper.selectByPrimaryKey(dtlId);
    }

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public List<CommonDicDtl> selectDetailByDicCodeAndKey(String dicCode,String dtlKey) {
        CommonDicDtlExample example = new CommonDicDtlExample();
        example.createCriteria().andDicCodeEqualTo(dicCode).andDtlKeyEqualTo(dtlKey);
        List<CommonDicDtl> dtls =  commonDicDtlMapper.selectByExample(example);
        return dtls;
    }
}
